package com.nl.trees;

public class HasSubTree {

	public static <T> boolean hasSubTree(BTree.Node<T> proot1,
			BTree.Node<T> proot2) {

		boolean result = false;
		if (proot1 != null && proot2 != null) {
			if (proot1.value == proot2.value) {
				result = doesTree1HasTree2(proot1, proot2);
				if (!result) {
					result=hasSubTree(proot1.left, proot2);
				}
				if (!result) {
					result=hasSubTree(proot1, proot2.right);
				}
			}
		}
		return result;
	}

	private static <T> boolean doesTree1HasTree2(BTree.Node<T> proot1,BTree.Node<T> proot2){
		if(proot2 ==null){
			return true;
		}
		if(proot1==null){
			return false;
		}
		if(proot1.value !=proot2.value){
			return false;
		}
		return doesTree1HasTree2(proot1.left, proot2.left) && doesTree1HasTree2(proot1.right, proot2.right);
	}
}
